﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhoneDBModel;

public partial class Admin_RaportIncasari : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["IsAdmin"] == null)
        {
            Response.Redirect("~/Login.aspx");
        }
        if (Page.IsPostBack == false)
        {
            TextBox1.Text = DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year;
            AfiseazaRaport();
        }
        
    }
    protected void btn_Click(object sender, EventArgs e)
    {      
        AfiseazaRaport();
    }

    void AfiseazaRaport()
    {
        int luna = int.Parse(TextBox1.Text.Substring(0, 2));
        int an = int.Parse(TextBox1.Text.Substring(3));
        litLuna.Text = TextBox1.Text;
        PhoneDBEntities dbe = new PhoneDBEntities();
        var list = from cl in dbe.CallLogs
                   where cl.Timestamp.Month == luna && cl.Timestamp.Year == an
                   group cl by new { cl.Timestamp.Day, cl.Timestamp.Month, cl.Timestamp.Year } into g
                   select new { Total = g.Sum(cl => cl.Cost), k = g.Key };

        List<VenitZilnic> lvz = new List<VenitZilnic>();
        decimal total = 0;
        foreach (var cl in list)
        {
            VenitZilnic vz = new VenitZilnic();
            vz.Day = cl.k.Day.ToString() + "." + cl.k.Month.ToString("00");
            vz.Total = (decimal)cl.Total;
            lvz.Add(vz);
            total += vz.Total;
        }

        VenitZilnic totalEntry = new VenitZilnic();
        totalEntry.Day = "Total";
        totalEntry.Total = total;
        lvz.Add(totalEntry);

        gvLastCalls.DataSource = lvz;
        gvLastCalls.DataBind();

        gvLastCalls.Rows[gvLastCalls.Rows.Count - 1].Font.Bold = true;
        
    }
    
}